﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Hec.TextMining
{
    public class KeyValuePairSerializer
    {
        public string Path { get; private set; }

        public KeyValuePairSerializer(string path)
        {
            var currentAppPath = AppDomain.CurrentDomain.BaseDirectory;
            currentAppPath = currentAppPath.Replace(@"\Tests\bin\Debug", "");
            Path = currentAppPath + path;
        }

        public void Serialize(List<KeyValuePair<Document, List<KeyValuePair<string, Cell>>>> list)
        {
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(list.GetType()/*, xRoot*/);
            
            System.IO.StreamWriter file = new System.IO.StreamWriter(Path, false, Encoding.UTF8);
            x.Serialize(file, list);
            file.Close();
        }

        public List<KeyValuePair<Document, List<KeyValuePair<string, Cell>>>> Deserialize()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<KeyValuePair<Document, List<KeyValuePair<string, Cell>>>>)/*, xRoot*/);
            TextReader tr = new StreamReader(Path, Encoding.UTF8);

            var inflate = (List<KeyValuePair<Document, List<KeyValuePair<string, Cell>>>>)serializer.Deserialize(tr);
            tr.Close();

            return inflate;
        }
    }
}
